﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinFormNativeDemos.Controls.PropertyGrid
{
    public partial class PropertyGrid绑定XML : Form
    {
        public PropertyGrid绑定XML()
        {
            InitializeComponent();
        }
        private Type GetType(string type)
        {
            Type _type = null;
            switch (type)
            {
                case "文本":
                    _type = typeof(string);
                    break;
                case "数字":
                    _type = typeof(decimal);
                    break;
                default:
                    _type = typeof(string);
                    break;
            }
            return _type;
        }
        /* xml节点格式
         <root>
            <item name="align" value="0" type="下拉框" desc="对齐方式" group="位置">
              <c value="left"/>
              <c value="center"/>
              <c value="right"/>
            </item>
            <item name="fontname" value="宋体" type="文本" desc="字体名称" group="格式"></item>
            <item name="fontsize" value="12" type="数字" desc="字体大小" group="格式"></item>
        </root>
        */
        private void Btn_SelectXML_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog fileDialog = new OpenFileDialog() { Filter="XML文件|*.xml" })
            {
                if (fileDialog.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        var ds = Helpers.XmlHelper.GetDataSet(fileDialog.FileName, Helpers.XmlHelper.XmlType.File);
                        if (ds.Tables.Count > 0)
                        {
                            XProps xps = new XProps();
                            DataTable master = ds.Tables["item"];
                            DataTable detail = ds.Tables["c"];
                            if (master != null)
                            {
                                foreach (DataRow row in master.Rows)
                                {
                                    XProp xprop = new XProp();
                                    xprop.Name = row["name"].ToString();
                                    xprop.Value = row["value"];
                                    xprop.Category = row["group"].ToString();
                                    xprop.Description = row["desc"].ToString();
                                    if (detail != null)
                                    {
                                        var rows = detail.Select($"item_Id={row["item_Id"]}");
                                        if (rows.Length > 0)
                                        {
                                            xprop.Value = Convert.ToInt32(row["value"]);
                                            string arr = "";
                                            rows.ToList().ForEach(p => arr += "," + p["value"]);
                                            xprop.ProType = typeof(CustomClass.MyComboItemConvert);
                                            xprop.Converter = new CustomClass.MyComboItemConvert(arr.Substring(1));
                                        }
                                        else
                                            xprop.ProType = GetType(row["type"].ToString());
                                    }
                                    else
                                        xprop.ProType = GetType(row["type"].ToString());
                                    //xprop.ReadOnly = true;//设置属性是否只读
                                    xps.Add(xprop);
                                }
                            }
                            propertyGrid1.SelectedObject = xps;
                        }
                    }
                    catch (Exception ex) { MessageBox.Show(ex.Message, "Error"); }
                }
            }
        }
    }
}
